package com.dc.refactoring._5_简化函数调用._5_ParameterizeMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * Description: 令函数携带参数
 * @author: gdc
 * @date: 2020/12/7
 * @version 1.0
 */
@Slf4j
public class Employeer {

    private double salary;

    /**
     * 提高百分之十
     */
    void tenPercentRaise(){
        salary *= 1.1;
    }

    /**
     * 提高百分之五
     */
    void fivePercentRaise(){
        salary *= 1.05;
    }

    /**
     * 提高
     * @param factor        因子
     */
    void raise(double factor) {
        salary *= (1 + factor);
    }


    // ---------------------------------------------------------


    // 改造前
    public double baseCharge(){
        double result = Math.min(lastUsage(), 100) * 0.03;
        if (lastUsage() > 100) {
            result += (Math.min(lastUsage(), 200) - 100) * 0.05;
        }
        if (lastUsage() > 200) {
            result += (lastUsage() - 200) * 0.07;
        }
        return result;
    }

    // 改造后
    public double baseCharge2(){
        double result = usageInRange(0, 100) * 0.03;
        result += usageInRange(100, 200) * 0.05;
        result += usageInRange(200, Integer.MAX_VALUE) * 0.07;
        return result;
    }

    private double usageInRange(int start, int end) {
        if (lastUsage() > start) {
            return Math.min(lastUsage(), end) - start;
        }
        return 0;
    }

    // 此处是一个获取数据的逻辑
    private int lastUsage() {
        return 202;
    }

    public static void main(String[] args){
        Employeer employeer = new Employeer();
        log.info(">>>>>>>> {}", employeer.baseCharge());
        log.info(">>>>>>>> {}", employeer.baseCharge2());
    }
}
